package com.menu;

import java.util.ArrayList;
import java.util.List;

public class MenuItem {
	
	private String text = "";
	
	private String href = "";
	
	private String target = "";
	
	private int level = 1;
	
	private List<MenuItem> children = new ArrayList<MenuItem>();
	
	private String index = "0";
	
	private String pindex = "-1";
	
	private int position = 0; // 0:顶部浮动菜单,1:右侧列表菜单
	
	private boolean isShow = true;
	
	private boolean isSelected = false;
	
	private int type = 0; // 0:列表，1：树形

	public MenuItem(String text, String href, int level) {
		super();
		this.text = text;
		if(href == null || "".equals(href)) {
			href = "";
		}
		this.href = href;
		this.level = level;
	}
	
	public MenuItem(String text, String href) {
		this(text, href, 1);
	}
	
	public MenuItem addChild(MenuItem item) {
		if(item == null) return this;
		
		item.pindex = this.index; 
		
		item.index = this.index + "_" + this.children.size();
		
		item.level = this.level + 1;
		
		this.children.add(item);
		
		return this;
	}
	
	public MenuItem addChild(String text, String href) {
		return this.addChild(text, href, "");
	}
	
	public MenuItem addChild(String text, String href, String target) {
		
		MenuItem item = new MenuItem(text, href);
		item.target = target;
		addChild(item);
		
		return this;
	}
	
	public MenuItem getItemByIndex(String index) {
		MenuItem item = null;
		
		if(index == null && "".equals(index)) {
			return item;
		}
		
		int idx = Integer.valueOf(index);
		
		item = this.children.get(idx);
		
		return item;
	}
	
	public MenuItem[] getChildren() {
		return this.children.toArray(new MenuItem[children.size()]);
	}
	
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getPindex() {
		return pindex;
	}

	public void setPindex(String pindex) {
		this.pindex = pindex;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public boolean isShow() {
		return isShow;
	}

	public void setShow(boolean isShow) {
		this.isShow = isShow;
	}

	public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
}
